package org.massdgames.thunder.server.domain.strategy;

import java.util.ArrayList;

import org.massdgames.thunder.server.domain.Card;
import org.massdgames.thunder.server.domain.CardType;
import org.massdgames.thunder.server.domain.Deck;
import org.massdgames.thunder.server.domain.FetchException;
import org.massdgames.thunder.server.domain.GuardianDeck;

import com.google.inject.Inject;

public class BasicFeatureFetchStrategy extends BasicStrategy implements FeatureFetchStrategy {

    private static final int DEFAULT_FEATURE_FETCH_COUNT = 1;

    private GuardianDeck guardianDeck;
    
    @Inject
    public BasicFeatureFetchStrategy(GuardianDeck guardianDeck) {
        this.setNumberOfCardsToFetch(DEFAULT_FEATURE_FETCH_COUNT);
        this.guardianDeck = guardianDeck;
    }

    @Override
    public ArrayList<Card> getCards(Deck deck) throws FetchException {
        ensureParameterValidity(deck);

        ArrayList<Card> fetchedCards = new ArrayList<Card>();

        while (fetchedCards.size() < getNumberOfCardsToFetch()) {

            Card fetchedCard = deck.getRandomCard();

            if (fetchedCard.getCardType() == CardType.PICK_TWO_SELECTOR) {
                System.out.println(" ** PICK TWO **");
                this.setNumberOfCardsToFetch(getNumberOfCardsToFetch() + 1);
                fetchedCards = getCards(deck);
                this.setNumberOfCardsToFetch(getNumberOfCardsToFetch() - 1);
            } else if (fetchedCard.getCardType() == CardType.GUARDIAN_SELECTOR) {
                System.out.println(" ** GUARDIAN **");
                Card guardianCard = guardianDeck.getRandomCard();
                fetchedCards.add(guardianCard);
            } else {
                fetchedCards.add(fetchedCard);
            }
        }
        return fetchedCards;
    }

}
